﻿namespace BM.Tools.WebControls.MForm.Controls.ValueFormatters
{
    using System;
    using System.Globalization;

    /// <summary>
    /// A formatter used to format the date
    /// </summary>
    public class DateFormatter : StringFormatter
    {
        /// <summary>
        /// Gets or sets the format that should be used. 
        /// This should be the same as the one used in <see cref="DateTime.ToString(string)"/> method.
        /// </summary>
        public string DateFormat
        {
            get;
            set;
        }

        /// <summary>
        /// Formats the date using the specified date format
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// the formatted string
        /// </returns>
        public override string Format(string value)
        {
            try
            {
                var date = Convert.ToDateTime(value, CultureInfo.InvariantCulture);
                return date.ToString(DateFormat, CultureInfo.InvariantCulture);
            }
            catch (FormatException)
            {
                return value;
            }
        }
    }
}
